<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class backend extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('mbackend');
		$this->sesina	= unserialize(base64_decode($this->session->userdata('ses_data')));
		$this->load->library(array("phpmailer",'encrypt'));
		
	}
	
	public function index()
	{
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		
		if($this->sesina){	
			$data=$this->sesina;
			$data["module"]='home';
			$data["data"]=$this->mbackend->get_data('tbl_user',$data["user_id"]);
			//print_r($this->sesina);
			show_backend('backend/home',$data);
		}
		else{
			$data["pesan"]="";
			$data["user"]="";
			$data["pass"]="";
			$this->load->view('login',$data);
		}
	}
	
	function get_data($p1="",$p2=""){
		$data=$this->sesina;
		echo $this->mbackend->get_data($p1,$p2,$data["user_id"]); 
		//test
		
	}
	
	function get_form($p1="",$p2=""){
		$data["user_id"]="";
		$data["group_user_id"]="";
		$data["nama_lengkap"]="";
		$data["email"]="";
		if($this->sesina){	
			$data=$this->sesina;
			switch($p1){
				case "home":
					$data["module"]='home';
					$data["data"]=$this->mbackend->get_data('tbl_user',$data["user_id"]);
					$this->load->view('backend/home',$data);
				break;
				
				case "user_toko":
					$data["module"]='toko_user';
					$data["data"]=$this->mbackend->get_data('tbl_toko_user',$data["user_id"]);
					//$data["toko_user"]=$data_toko[]
					//print_r($data);
					//echo $data["jmlna"];
					$this->load->view('backend/toko_user',$data);
					
				break;
				case "reseller":
					$this->load->view('backend/data_reseller',$data);
				break;
				case "ubah_pwd":
					$this->load->view('backend/ubah_password',$data);
				break;
				
				case "warna":
				case "kategori":
				case "portofolio":
				case "narsis":
				case "aktivasi":
				case "syarat":
				case "faq":
					$this->load->view('backend/'.$p1,$data);
				break;
				
				
				case "produk":
					$this->load->view('backend/data_produk',$data);
				break;
				
				case "rating":
					$data["id"]=$this->input->post('id');
					$this->load->view('backend/rating',$data);
				break;
				
				case "toko":
					$this->load->view('backend/data_toko',$data);
				break;
				case "lihat_toko":
					$data["id"]=$this->input->post('id');
					$this->load->view('backend/list_produk',$data);
				break;
				
				case "user_produk":
					$data["data"]=$this->mbackend->get_data('tbl_toko_user',$data["user_id"]);
					$this->load->view('backend/user_produk',$data);
				break;
				default:$this->load->view('backend/underconstraction',$data);
			}
		
		}
		
		else{
			$data["pesan"]="";
			$data["user"]="";
			$data["pass"]="";
			$this->load->view('login',$data);
		}
	}
	
	function get_crud($p1="",$p2="",$p3=""){
		$data=$this->sesina;
		switch($p1){
			case "user_toko":
				if($p2=='edit'){
						$data["data"]=$this->mbackend->get_data('tbl_toko_user',$data["user_id"]);
						$data["sts"]='edit';
				}
				else{
						$data["sts"]='add';
				}
				$this->load->view('backend/form_toko_user',$data);
			break;
			
			case "user_produk":
				if($p2=='edit'){
						$data["data"]=$this->mbackend->get_data('tbl_produk_user',$p3,'edit');
						$data["foto_1"]=$data["data"]->foto_1;
						$data["foto_2"]=$data["data"]->foto_2;
						$data["foto_3"]=$data["data"]->foto_3;
						$data["sts"]='edit';
				}
				else{
						$data["foto_1"]="tidak_foto.jpg";
						$data["foto_2"]="tidak_foto.jpg";
						$data["foto_3"]="tidak_foto.jpg";
						$data["sts"]='add';
				}
				//print_r($data["data"]);
				$this->load->view('backend/form_user_produk',$data);
			break;
			
			case "tbl_portofolio":
				if($p2=='edit'){
						$data["data"]=$this->mbackend->get_data('tbl_portofolio',$p3);
						$data["sts"]='edit';
				}
				else{
						$data["sts"]='add';
				}
				$this->load->view('backend/form_porto_folio',$data);
			break;
			
			case "tbl_narsis":
				if($p2=='edit'){
						$data["data"]=$this->mbackend->get_data('tbl_narsis',$p3);
						$data["sts"]='edit';
				}
				else{
						$data["sts"]='add';
				}
				$this->load->view('backend/form_narsis',$data);
			break;
			
			case "tbl_faq":
				if($p2=='edit'){
						$data["data"]=$this->mbackend->get_data('tbl_faq',$p3);
						$data["sts"]='edit';
				}
				else{
						$data["sts"]='add';
				}
				$this->load->view('backend/form_faq',$data);
			break;
			case "cl_warna":
				$data["sts"]=$p2;
				$ex=explode("_",$p3);
				if($p2=='edit'){
					$data["id_na"]=$ex[0]; $data["warna"]=$ex[1];
				}
				$this->load->view('backend/form_warna',$data);
			break;
			case "cl_kategori":
				$data["sts"]=$p2;
				$ex=explode("_",$p3);
				if($p2=='edit'){
					$data["id_na"]=$ex[0]; $data["kategori"]=$ex[1];
				}
				$this->load->view('backend/form_kategori',$data);
			break;
			case "tbl_faq":
				$data["sts"]=$p2;
				$ex=explode("_",$p3);
				if($p2=='edit'){
					$data["id_na"]=$ex[0]; $data["kategori"]=$ex[1];
				}
				$this->load->view('backend/form_faq',$data);
			break;
		}
		
			
	}
	
	function simpan($p1="",$p2=""){
		
		$post = array();
		$data["msg"]="";
		$data["id"]="";
		foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
		switch($p1){
			case "tbl_toko_user":
				$upload=$this->upload($p1);
				if($upload==1){
					$data["msg"]=1;
					$data["id"]="";
					$filename = basename($_FILES['logo']['name']);
					$tmp = explode(".", $filename);
					$post["logo"] = $this->sesina["user_id"].'/'.$this->sesina["user_id"].'.'.$tmp[1];
					
				}
			break;
			
			case "tbl_porto_folio":
				$data["msg"]='';
				$data["id"]="";
				$date=date('YmdHis');
				$upload=$this->upload($p1,$date);
				if($upload==1){
					$filename = basename($_FILES['gambar_porto_folio']['name']);
					$tmp = explode(".", $filename);
					$post["gambar_porto_folio"] = $date.'.'.$tmp[1];
					$data["msg"]=1;
					$data["id"]="";
					
				}
				if($data["msg"]==1){$this->mbackend->simpan($p1,$post);}
				echo json_encode($data);exit;
			break;
			
			case "tbl_narsis":
				$data["msg"]='';
				$data["id"]="";
				$date=date('YmdHis');
				$upload=$this->upload($p1,$date);
				if($upload==1){
					$filename = basename($_FILES['gambar_narsis']['name']);
					$tmp = explode(".", $filename);
					$post["gambar_narsis"] = $date.'.'.$tmp[1];
					$data["msg"]=1;
					$data["id"]="";
					
				}
				if($data["msg"]==1){$this->mbackend->simpan($p1,$post);}
				echo json_encode($data);exit;
			break;
			
			case "tbl_produk_user":
				$toko=$this->mbackend->get_data("tbl_toko_user",$this->sesina["user_id"]);
				$post["tbl_toko_user_id"]=$toko["toko_user"]->id;
				$data["msg"]=1;
				$data["id"]="";
						for($i=1;$i<=3;$i++){
							$file="foto_".$i;
							$filename = basename($_FILES[$file]['name']);
							if($filename!=''){
								if($post["sts"]=='edit'){
									$res=$this->db->get_where('tbl_produk_user',array("id"=>$post["id"]))->result_array();
									$path="repositori/produk_user/".$res[0][$file];
									
									if(file_exists($path)){chmod($path,0777);unlink($path);}
								}
								$tmp = explode(".", $filename);
								$date=date('YmdHis');
								$post["foto_".$i]=$this->sesina["user_id"]."/".$this->sesina["user_id"].'_'.$date.'_'.$i.'.'.$tmp[1];
								$upload=$this->upload($p1,$file,$i);
								if($upload==1){
										$data["msg"]=1;
										$data["id"]="";
								}
								else{
									$data["msg"]=2;
									$data["id"]="";
								}
							}
						}
				if($data["msg"]==1){$this->mbackend->simpan($p1,$post);}
				echo json_encode($data);exit;
				
			break;
			
			case "tbl_pesan_email":
				$lokasi = array();
				if($p2!=''){
					foreach($_FILES as $v=>$x){
					$fileElementName=$v;
					if($v=='design'){$des='design_depan';}
					else{$des='design_belakang';}
						if($x["name"] !=''){
								$tmp = explode(".", $x["name"]);
								$date=date('YmdHis');
								$email = explode("@",$this->input->post('email'));
								$cek = $this->mbackend->get_data('tbl_pesan_email',$this->input->post('email'));
								$jml_cek = count($cek);
								$no = $jml_cek+1;
								$nama_file=$email[0].'_'.$no.'_'.$des;
								$post[$v]=$email[0].'_'.$no.'_'.$des.'.'.$tmp[1];
								$lokasi[]='repositori/cetak_sendiri/'.$post[$v];
								
								$upload=$this->upload($p1,$nama_file,$v);
								if($upload==1){
										$data["msg"]=1;
										$data["id"]="";
								}
								else{
									$data["msg"]=2;
									$data["id"]="";
								}
						}
					}		
					if($data["msg"]==1){
						
						if($this->mbackend->simpan($p1,$post)){
							echo $this->kirim_email($post,$lokasi,'sendiri');exit;
						}
					}
					echo json_encode($data);exit;
				
				}else{
					if($this->mbackend->simpan($p1,$post)){
						echo $this->kirim_email($post);exit;
					}
				}
			break;
			
			case "tbl_user":
				//print_r($post);exit;
				if($post["sts"]=='edit'){
					if($this->input->post("password")){
						$newPass = $this->encrypt->encode($post['password']);
						unset($post['password']);
						$post['password']=$newPass;
					}
					else{
						$file="foto";
						$filename = basename($_FILES[$file]['name']);
						$tmp=explode(".",$filename);
						$date=date('YmdHis');
						$upload=$this->upload($p1,$date);
						$post["foto"]=$this->sesina["user_id"].".".$tmp[1];
						if($upload==1){echo $this->mbackend->simpan($p1,$post);exit;}
					}
					
					
				}
				//$this->mhome->changePass($this->auth['USER_ID'], $newPass);
			break;
			
					//default: echo $this->mbackend->simpan($p1,$post);
			
			
		}
		
		if($this->mbackend->simpan($p1,$post))
			{
				$data["msg"]=1;
				$data["id"]="";
			}
		echo json_encode($data);exit;
		//echo $this->mbackend->simpan($p1,$post);
	}
	
	function upload($p1="",$p2="",$p3=""){

		$date=date('YmdHis');
		
		switch ($p1){
			case "tbl_toko_user":
				$fileElementName='logo';
				$upload_dir="repositori/logo_toko/".$this->sesina["user_id"].'/';
				$newFilename = $this->sesina["user_id"];
			break;
			
			case "tbl_porto_folio":
				$fileElementName='gambar_porto_folio';
				$upload_dir="repositori/porto_folio/";
				$newFilename = $p2;
			break;
			
			case "tbl_narsis":
				$fileElementName='gambar_narsis';
				$upload_dir="repositori/narsis/";
				$newFilename = $p2;
			break;
			
			case "tbl_user":
				$fileElementName='foto';
				$upload_dir="repositori/user_profil/";
				$newFilename = $this->sesina["user_id"];
			break;
			
			case "tbl_pesan_email":
				$fileElementName=$p3;
				$upload_dir="repositori/cetak_sendiri/";
				$newFilename = $p2;
			break;
			
			case "tbl_produk_user":
				$fileElementName=$p2;
				$upload_dir="repositori/produk_user/";
				$upload_dir .=$this->sesina["user_id"].'/';
				$newFilename = $this->sesina["user_id"].'_'.$date.'_'.$p3;
			break;
		}
		//print_r($this->sesina);exit;
		$fName = $_FILES[$fileElementName]['tmp_name'];
		$fSize = @filesize($_FILES[$fileElementName]['tmp_name']);
		@unlink($_FILES[$fileElementName]);		
		$filename = basename($_FILES[$fileElementName]['name']);
		$tmp = explode(".", $filename);
		$newFilename .= '.'.$tmp[1];
		$uploadfile = $upload_dir . $newFilename;
		if(!file_exists($upload_dir))mkdir($upload_dir, 0777, true);
		if(file_exists($uploadfile)){chmod($uploadfile,0777);unlink($uploadfile);}
		
		move_uploaded_file($_FILES[$fileElementName]['tmp_name'], $uploadfile);
		
		if(!chmod($uploadfile, 0775)){
			$data["msg"]=2;
			$data["id"]="";
			echo json_encode($data);exit;
		}
		else{
			$a=1;
			return $a;
		}
		
		
	}
	
	function get_combo($p1,$valfilter="",$val="",$val2=""){
		$optTemp =""; 
		switch($p1){
				case 'cl_warna':
					$rec=$this->mbackend->isi_combo('cl_warna','id','warna');
				break;
				
				case 'cl_kategori':
					$rec=$this->mbackend->isi_combo('cl_kategori','id','kategori');
				break;
		}
		
		$optTemp .= "<option value=''>-- Pilih --</option>";			
			foreach($rec as $v=>$k)
			{
				if($this->input->post("v") == $k['id'])
					$optTemp .= "<option selected value='".$k['id']."'>".$k['text']."</option>";
				else 
					$optTemp .= "<option value='".$k['id']."'>".$k['text']."</option>";
			}
			echo $optTemp;	
		
	}
	
	function hapus($p1){
		echo $this->mbackend->hapus($p1);
	}
	
	function kirim_email($data="",$lokasi='',$sts=""){
			
			//print_r($data);exit;
			$this->load->model('mdata');
			$produk=$this->mdata->get_data("kirim_email",$data["tbl_produk_id"]);
			if($sts=='sendiri'){
				$file='tidak_foto.jpg';
			}
			else{$file='produk_user/'.$produk->foto_1;}
			try {
				$body             			= '
				<style type="text/css">
				#table-2 {
					border: 1px solid #e3e3e3;
					background-color: #f2f2f2;
						width: 100%;
					border-radius: 6px;
					-webkit-border-radius: 6px;
					-moz-border-radius: 6px;
				}
				#table-2 td, #table-2 th {
					padding: 5px;
					color: #333;
				}
				#table-2 thead {
					font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
					padding: .2em 0 .2em .5em;
					text-align: left;
					color: #4B4B4B;
					background-color: #C8C8C8;
					background-image: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#e3e3e3), color-stop(.6,#B3B3B3));
					background-image: -moz-linear-gradient(top, #D6D6D6, #B0B0B0, #B3B3B3 90%);
					border-bottom: solid 1px #999;
				}
				#table-2 th {
					font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
					font-size: 14px;
					line-height: 20px;
					font-style: normal;
					font-weight: normal;
					text-align: center;
					text-shadow: white 1px 1px 1px;
				}
				#table-2 td {
					line-height: 20px;
					font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
					font-size: 10px;
					
					border-bottom: 1px solid #fff;
					border-right: 1px solid #fff;
					border-top: 1px solid #fff;
				}
				#table-2 td:hover {
					background-color: #fff;
				}
				.gambar_kecil{background-color:#FFFFFF;border:1px dashed #CCCCCC; padding:2px; margin:5px;box-shadow: 3px 3px 3px 0px #333333;}
				</style>
				<h2>Pemesanan Online ( '.($sts=="sendiri" ? 'Cetak Design Sendiri' : 'Cetak Produk' ).' )</h2><hr>';
				$body .='
				<div style="margin-left:3px; padding-top:4px; width:90%;text-shadow: 2px 2px 2px #ccc;font-weight: bold;font-size: 12px; ">
					kepada admin bikinkaosbebas.com,<br><br> dibawah ini daftar pemesanan harap untuk dikonfirmasi dan dikerjakan sesuai dengan permintaan dan jumlah yang diminta <br> Mohon untuk ditindak lanjuti
					
				</div><br>
				<table width="100%" id="table-2" border="0" margin="5" cellspacing="0" cellpadding="0">
						  <tr>
							<td width="2%">1.&nbsp;</td>
							<td width="21%"><b>Nama Produk</b></td>
							<td width="1%">:</td>
							<td width="51%">'.($sts=="sendiri" ? 'Cetak Design Sendiri' : $produk->nama_produk).'</td>
						    <td width="25%" rowspan="6" align="center"><img src="'.base_url().'repositori/'.$file.'" height="100" width="100" class="gambar_kecil" >&nbsp;</td>
						  </tr>
						  <tr>
						    <td>2.&nbsp;</td>
						    <td><b>Alamat</b></td>
						    <td>:</td>
						    <td>'.$produk->alamat.'</td>
					    </tr>
						  <tr>
						    <td>3.&nbsp;</td>
						    <td><b>No Telephone</b></td>
						    <td>:</td>
						    <td>'.$produk->telp.'</td>
					    </tr>
						  <tr>
						    <td>4.&nbsp;</td>
						    <td><b>No Handphone</b></td>
						    <td>:</td>
						    <td>'.$produk->hp.'</td>
					    </tr>
						  <tr>
						    <td>5.&nbsp;</td>
						    <td><b>Email Pemesan</b></td>
						    <td>:</td>
						    <td>'.$produk->email.'</td>
					    </tr>
						  <tr>
						    <td>6.&nbsp;</td>
						    <td><b>Jumlah Cetak</b></td>
						    <td>:</td>
						    <td>'.$produk->jml.'</td>
					    </tr>
						  <tr>
						    <td>7.&nbsp;</td>
						    <td><b>Tanggal Pemesanan</b></td>
						    <td>:</td>
						    <td>'.$produk->tgl_buat.'</td>
						    <td align="center">'.($sts=="sendiri" ? 'File Dalam Attachment' : 'Nama Toko : '.$produk->nama_toko).'&nbsp;</td>
					    </tr>
						  <tr>
						    <td>8.&nbsp;</td>
						    <td><b>Comment</b></td>
						    <td>:</td>
						    <td>'.$produk->comment.'</td>
						    <td align="center">'.($sts=="sendiri" ? '' : 'Reseller By : '.$produk->nama_lengkap).' &nbsp;</td>
					    </tr>
						</table>';
				
				if($this->config->item('SMTP')) $this->phpmailer->IsSMTP();
				//$this->phpmailer->Host       = $this->config->item('Host'); 
				//$this->phpmailer->SMTPDebug  = 2;                                  
				$this->phpmailer->SMTPAuth   = $this->config->item('SMTPAuth');       
				$this->phpmailer->SMTPSecure = "tls";
				$this->phpmailer->Port       = $this->config->item('Port');                    
				$this->phpmailer->Host       = $this->config->item('Host'); 
				$this->phpmailer->Username   = $this->config->item('Username');     
				$this->phpmailer->Password   = $this->config->item('Password');            

				$this->phpmailer->AddReplyTo($this->config->item('EmaiFrom'),$this->config->item('EmaiFromName'));

				$this->phpmailer->From       = $this->config->item('EmaiFrom');
				$this->phpmailer->FromName   = $this->config->item('EmaiFromName');

				$to = "yogi_p4try4@yahoo.com";
				$this->phpmailer->AddAddress($to);
				$this->phpmailer->Subject   = "Pemesanan Online";
				$this->phpmailer->AltBody   = "To view the message, please use an HTML compatible email viewer!"; 
				$this->phpmailer->WordWrap  = 100; 
				$this->phpmailer->MsgHTML($body);
				$this->phpmailer->IsHTML(true);
				if(count($lokasi)>0){
					foreach($lokasi as $x){
						$this->phpmailer->AddAttachment($x);
					}
					
				}
				
				//$this->phpmailer->AddAttachment('repositori/cetak_sendiri/yogi_p4try4_1_design_belakang.png');
				//$this->phpmailer->AddAttachment('repositori/cetak_sendiri/yogi_p4try4_1_design_depan.png');
				$this->phpmailer->Send();
				return 1;
			} catch (phpmailerException $e) {
				return $e->errorMessage();
			}
			
		//	mail( "yourname@example.com", "Email Subject", 'HAHAHAHAHA', "From: $email" );
	}
	
	function aktif(){
		$post = array();
		foreach($_POST as $k=>$v) $post[$k] = $this->input->post($k);
		
		$id=$post["id"];
		unset($post["id"]);
		$this->db->where('id',$id);
		echo $this->db->update('tbl_produk_user',$post);
		
	}
	function set_flag(){
		$id=$this->input->post("id");
		//unset($post["id"]);
		$post=array("flag"=>$this->input->post("flag"));
		$this->db->where('id',$id);
		echo $this->db->update('tbl_pesan_email',$post);
	}
	
}
